﻿//======================================================================
//
//        Copyright (C) 2015   
//        All rights reserved
//
//        filename : PutOutAllocation
//        description : 半成品/成品卡调拨出库
//        created by XXX at  20150821
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using CardLogistics.Model.CardMaking;
using System.Data;
using System.Text;
using CardLogistics.DAL.PutOutCard;

namespace CardLogistics.Web.MainPage.PutOutCard.PutOutAllocation
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.PutOutCard.PutOutAllocationData DAL = new DAL.PutOutCard.PutOutAllocationData();
        private static readonly CardLogistics.DAL.PutOutCard.PutInAllocationData DDAL = new DAL.PutOutCard.PutInAllocationData();
        /// <summary>
        /// 盒号集合
        /// </summary>
        protected Dictionary<string, string> CardNOList = null;
        private static DataTable DtBox;//存放盒号等信息
        public string TitleName
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CardNOList = new Dictionary<string, string>();
            if (!IsPostBack)
            {
                ALLOCATIONDATE.Value = DateTime.Now.ToString("yyyy-MM-dd");//调出日期
                BindGroup();//绑定领用机构列表
                TitleName = GetStatusInfo(-1, "半成品/成品卡调拨出库");
                InitControl(sender, e);
            }
        }

        #region 绑定领用机构列表
        /// <summary>
        /// 绑定根节点
        /// </summary>
        void BindGroup()
        {
            DataTable dt = new DAL.Basic.Basic_SalesInfoData().GetList(" ISDELETE=0 ", "*");//获取所有节点
            if (dt != null && dt.Rows.Count > 0)
            {
                LEADINGMECHANISM.DataSource = dt;
                LEADINGMECHANISM.DataTextField = "SALESNAME";
                LEADINGMECHANISM.DataValueField = "Id";
                LEADINGMECHANISM.DataBind();
            }
        }
        #endregion

        #region 编辑时绑定值
        /// <summary>
        /// 编辑时绑定值
        /// </summary>
        public void InitControl(object sender, EventArgs e)
        {
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    CardLogistics.Model.PutOutCard.PutOutAllocation pData = DAL.GetModel(_key);
                    SetWebControls(this.Page, pData);
                    LEADINGMECHANISM.SelectedValue = pData.LEADINGMECHANISM; //领用机构
                    hf_PutInAllocationId.Value = pData.PUTINALLOCATIONID; //调拨入库单号Id
                    //根据验收入库外键查询入库明细
                    DataTable dt = DAL.GetInfoByOutAllocationId(_key);
                    DataView dv = dt.DefaultView;
                    dv.Sort = "StartNo ASC,EndNo ASC";
                    dt = dv.ToTable();
                    string detId = "";

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            detId += dr["CardNo"].ToString() + "|" + dr["startno"].ToString() + "|" + dr["endno"].ToString() + "|" +
                                     dr["DeservedNumber"].ToString() + "|" + dr["actualnumber"].ToString() + "|" + dr["missingcardno"].ToString() + ";";
                        }
                        hf_CheckId.Value = detId.TrimEnd(';');
                        btn_PutInId_Click(sender, e);//获取盒号等信息
                    }
                    btn_Add.Visible = btn_Submit.Visible = false;
                    if (pData.IsSubmit == 0 || pData.IsSubmit == 3)//状态为 0(未提交) 或 3(驳回)时才可以进行编辑操作
                    {
                        btn_Add.Visible = btn_Submit.Visible = true;
                    }
                }
            }
            else
            {
                PUTOUTALLOCATIONNO.Value = GetBillNumber("半成品/成品卡调拨出库");
            }
        }
        #endregion

        #region 保存按钮事件
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(0);
                ShowMegByResult(result, 0);
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        #endregion

        #region 保存并提交事件
        /// <summary>
        /// 保存并提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(1);
                ShowMegByResult(result, 1);
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        #endregion

        #region 提示信息
        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="result"></param>
        private void ShowMegByResult(string result, int issubmit)
        {
            if (result.Split(',')[0] == "ok")
            {
                if (issubmit == 1)
                {
                    InsertAppInfo(16, result.Split(',')[1]);// 16半成品/成品卡调拨出库
                    InsertLog("保存并提交半成品/成品卡调拨出库信息,调拨出库单号:" + PUTOUTALLOCATIONNO.Value);
                }
                else
                    InsertLog("保存半成品/成品卡调拨出库信息,调拨出库单号:" + PUTOUTALLOCATIONNO.Value);
                ReturnListInfo();
            }
            else
            {
                ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
            }
        }
        #endregion

        #region 半成品/成品卡调拨出库信息
        /// <summary>
        ///半成品/成品卡调拨出库信息
        /// </summary>
        /// <returns></returns>
        private string EncapsulationModel(int issubmit)
        {
            string result = string.Empty;
            CardLogistics.Model.PutOutCard.PutOutAllocation model = new CardLogistics.Model.PutOutCard.PutOutAllocation();
            model = GetWebControls<CardLogistics.Model.PutOutCard.PutOutAllocation>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;
            model.LASTUPDATEDATE = DateTime.Now;
            model.ADDTIME = DateTime.Now;
            model.PUTINALLOCATIONID = hf_PutInAllocationId.Value; //外键调拨入库Id
            model.LEADINGMECHANISM = LEADINGMECHANISM.SelectedValue;//领用机构
            string FileValue = hf_CheckId.Value; //调拨出库明细字段
            model.IsSubmit = issubmit;
            if (IsEdit)
            {
                model.ID = Request.QueryString["id"];
                result = DAL.Update(model, FileValue);
            }
            else
            {

                result = DAL.Add(model, FileValue);
                if (result.Split(',')[0] == "ok")
                {
                    ChgBillNumber("半成品/成品卡调拨出库");
                }
            }
            return result;
        }
        #endregion

        #region 通过半成品/成品出库单ID获取详细信息
        /// <summary>
        /// 通过调拨入库单ID获取详细信息事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_PutInId_Click(object sender, EventArgs e)
        {
            if (hf_PutInAllocationId.Value != "")
            {
                //根据调拨入库Id获取基本信息
                DtBox = DDAL.GetInfoByPutInId(hf_PutInAllocationId.Value);
                DataView dv = DtBox.DefaultView;
                dv.Sort = "StartNo ASC,EndNo ASC";
                DtBox = dv.ToTable();
                CardNOList = new Dictionary<string, string>();
                foreach (DataRow dr in DtBox.Rows)
                {
                    //开始卡号|结束卡号|应有数量|实际数量|缺失卡号
                    string idStr = dr["StartNo"].ToString() + "|" + dr["EndNo"].ToString() + "|" + dr["DeservedNumber"].ToString() + "|" + dr["ActualNumber"].ToString() + "|" + dr["MissingCardNo"].ToString();
                    CardNOList.Add(idStr, dr["CardNo"].ToString().Trim());
                }
            }
        }
        #endregion
    }
}